use strict;
use warnings;
use Test::More tests => 4;
BEGIN {
    use_ok 'Cammy::Util' => qw(is_positive_int is_valid_filename http_client verify_config);
}

subtest http_client => sub {
    plan tests => 1;
    isa_ok http_client, 'HTTP::Tiny';
};

subtest is_positive_int => sub {
    plan tests => 7;
    ok is_positive_int(1);
    ok is_positive_int(2);
    ok is_positive_int(9e10);
    ok !is_positive_int(-1);
    ok !is_positive_int(1.1);
    ok !is_positive_int(-1.4);
    ok !is_positive_int("1/4");
};

subtest 'config-ok' => sub {
    my $config = {
          'logo' => 'dal-logo-black.png',
          'run_interval' => '10',
          'cron_interval' => '1',
          'timeout' => '8',
          'webcams' => {
                         'dalsub' => {
                                       'blur_sigma' => '10',
                                       'longitude' => '64.12W',
                                       'blur_radius' => '7',
                                       'max_file_size_diff' => '302400',
                                       'sunset_easement' => 2,
                                       'sunrise_easement' => 2,
                                       'frame_rate' => 15,
                                       'bit_rate' => 15,
                                       'timelapse_size' => 'vga',
                                       'latitude' => '44.87N',
                                       'image_url' => 'http://129.173.68.11/jpg/1/image.jpg',
                                       'weather_url' => 'http://dd.weatheroffice.gc.ca/citypage_weather/xml/NS/s0000318_e.xml',
                                       'location_name' => 'Halifax',
                                       'shape' => 'rectangle 0,278 500,550'
                                     }
                       },
          'out_dir' => '/tmp/cammy'
        };
    eval { verify_config($config) };
    ok !$@, 'no errors in validating config';

    {
        local $config->{run_interval} = 'not an integer';
        eval { verify_config($config) };
        like $@, qr{run_interval}, 'run_interval error uncovered';
    }
    {
        local $config->{webcams}->{dalsub}->{blur_sigma} = 'not an integer';
        eval { verify_config($config) };
        like $@, qr{blur_sigma}, 'blur_sigma error uncovered';
    }
    {
        local $config->{webcams}->{dalsub}->{image_url} = 'not a url';
        eval { verify_config($config) };
        like $@, qr{image_url}, 'image_url error uncovered';
    }
};
